//
//  SDianyingViewController.m
//  News
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SDianyingViewController.h"
#import "SFDetailsViewController.h"
#import "SFilmCollectionViewCell.h"
#import "SfilmObject.h"
#import "SNetWorkHandle.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
@interface SDianyingViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)NSString *title;
@property(nonatomic, assign)NSInteger average;
@property(nonatomic, retain)UIImageView *images;
@property(nonatomic, retain)SfilmObject *Sf;
@property(nonatomic, retain)NSString *id;
@property(nonatomic, retain)NSMutableArray *ArrOne;
@property(nonatomic, retain)NSMutableArray *ArrTwo;
@property(nonatomic, retain)NSString *str;
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@property(nonatomic ,retain)UIImageView *imageView;
@end

@implementation SDianyingViewController
- (void)dealloc
{
    [_imageView release];
    [_str release];
    [_id release];
    [_images release];
    [_arr release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.ArrOne = [NSMutableArray array];
    self.ArrTwo  =[NSMutableArray array];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
 
    
    
    
    self.title  =@"电影";
  self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style:UIBarButtonItemStyleDone target:self action:@selector(fanHui)];
    [self create];
    
    //请求出现小菊花
    self.imageView  = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _imageView.image = [UIImage imageNamed:@"5EA2D64D-15BE-43D8-AB18-E15BCE039C8F"];
    _imageView.alpha =1;
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [_activityIndicator setCenter:_imageView.center];
    _activityIndicator.backgroundColor= [UIColor grayColor];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [_imageView addSubview:_activityIndicator];
    
    [self.view addSubview:_imageView];
    
  [self dianying1];
    
//先默认下拉
    
    self.isUpLoading = NO;
    //[self addHeader];
    
}

-(void)fanHui
{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}
// 解析数据
-(void)dianying1
{
    [_activityIndicator startAnimating];
    [SNetWorkHandle  getDataWithURL:@"http://api.douban.com/v2/movie/nowplaying?alt=json&apikey=0df993c66c0c636e29ecbb5344252a4a&app_name=doubanmovie&city=" completion:^(id result) {
        
        _ArrOne = [result objectForKey:@"entries"];
        
        
        // 遍历字典
        for (NSDictionary *dic in _ArrOne) {
       self.Sf   = [[SfilmObject alloc]initWithDic:dic];
            
            [_ArrTwo addObject:_Sf];
        }

        
        [self.collectionView headerEndRefreshing];//结束头部刷新效果
        
        //刷新tableview
        [self.collectionView reloadData];
        [_imageView removeFromSuperview];
    }];
}

-(void)create{
    
    // 瀑布流
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // item 大小
    flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width/ 4, [UIScreen mainScreen].bounds.size.height/ 3.8);
    // 最小行间距
    flowLayout.minimumLineSpacing = 10;
    // 最小列间距
    flowLayout.minimumInteritemSpacing = 10;
    // 边界
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10,10, 10);
    // 创建
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    // 代理
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 50);
    [self.view addSubview:_collectionView];
    [_collectionView release];
    [flowLayout release];
//#warning    注册一个cell
  
    [_collectionView registerClass:[SFilmCollectionViewCell class] forCellWithReuseIdentifier:@"Sfilm"];
    
}
#pragma mark - collectionView协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return _ArrTwo.count ;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    SFilmCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Sfilm" forIndexPath:indexPath];

    cell.Sfilm = _ArrTwo[indexPath.item];

    return cell;
}

//push到电影详情界面
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    SFDetailsViewController *details = [[SFDetailsViewController alloc] init];
    
    SfilmObject * temp = [_ArrTwo objectAtIndex:indexPath.row];
    details.str = temp.myid;
    details.Mytitle = temp.Mytitle;
    [self.navigationController pushViewController:details animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
